package parsing;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

public class KochhomeParser {
	private static String[][] removeFR = new String[][]{
		{ // Lektion 1
			"les sanitaires",
			"Tiens",
			"un jeu de sons",
			"la salle des élèves",
		},
		{ // Lektion 2
			"une marelle",
			"une papeterie",
			"un square",
			"ben",
			"devant",
		},
	};

	private static ModifyEntry[][] changeFRFR = new ModifyEntry[][]{
		{ // Lektion 1
			new ModifyEntry("collège","école secondaire"),
			new ModifyEntry("Moi, je m'appelle Thomas!","Je m'appelle Thomas!"),
			new ModifyEntry("Toi, tu es Victor?","Tu es Victor?"),
			new ModifyEntry("Bonjour, monsieur!","Bonjour! (général)"),
			new ModifyEntry("Bonjour, madame!","Bonjour! (général)"),
			new ModifyEntry("un principal","un directeur d'école"),
			new ModifyEntry("son","son (J'entends un son!)"),
			new ModifyEntry("un","un (le chiffre)"),
			new ModifyEntry("un professeur d'Allemand (f)","une professeure d'Allemand"),
		},
		{ // Lektion 2
			new ModifyEntry("au collège","à l'école secondaire"),
			new ModifyEntry("une gomme","une efface"),
			new ModifyEntry("un accueil","un accueil (Réception, Lobby)"),
			new ModifyEntry("entrer","entrer (le verbe)"),
		},
	};
	private static ModifyEntry[][] changeDEDE = new ModifyEntry[][]{
		{ // Lektion 1
			new ModifyEntry("Null","null"),
		},
		{ // Lektion 2
			new ModifyEntry("etw. (an)kleben","kleben"),
		},
	};
	
	
	public static List<TranslateEntry> getLessonContent(int i){
		List<TranslateEntry> list = new ArrayList<TranslateEntry>();
		String url = "http://www.kochhome.de/cgi-bin/baseportal.pl?htx=/vokabeltrainer/vokabelliste&ziel=&riz=&spr=fr&sortz=8&Lektion="+i+"&Thema2=&was=det";
		//out.println(url);
		try {
			BufferedReader reader = WebsiteReader.read(url);
			StringBuilder src = new StringBuilder();
			String line = reader.readLine();
			
			while (line != null) {
				src.append(line);//.append(newline);
				line = reader.readLine(); }
			String all = src.toString();
			//out.println(all);
			String debut = "<!-- Tabelle Detail Anfang -->  ";
			String fin = "<!-- Tabelle 1 Ende --> ";
			String content = all.substring(all.indexOf(debut)+debut.length(),all.lastIndexOf(fin));
			//out.println(content);
			String nonViewedContent = content;
			String rowDebut = "<tr>";
			String rowEnd = "</tr>";
			String cellLeftDebut = "<td valign=\"top\">";
			String cellRightDebut = "<td valign='top'>";
			String lilNoteDebut = "<font color='#008000'>";
			String lilNoteEnd = "</font>";
			String cellNoteDebut = "<td colspan='2'><font color='#FF0000'>";
			String cellEnd = "</td>";
			String cellNoteEnd = "</font></td>";
			TranslateEntry lastEntry = null;
			while(nonViewedContent.contains(rowDebut))
			{
				int iRowDeb = nonViewedContent.indexOf(rowDebut);
				int iRowFin = nonViewedContent.indexOf(rowEnd, iRowDeb);
				String row = nonViewedContent.substring(iRowDeb+rowDebut.length(),iRowFin);
				nonViewedContent = nonViewedContent.substring(iRowFin+rowEnd.length()).trim();
				if(lastEntry != null && row.contains(cellNoteDebut))
				{
					String note = row.substring(row.indexOf(cellNoteDebut)+cellNoteDebut.length(),row.indexOf(cellNoteEnd)).trim();
					lastEntry.setNote(note);
					//out.println("Note: " + note);
				}
				else if(row.contains(cellLeftDebut) && row.contains(cellRightDebut))
				{
					String germanPart = row.substring(row.indexOf(cellLeftDebut)+cellLeftDebut.length(),row.indexOf(cellEnd)).trim();
					String frenchPart = row.substring(row.indexOf(cellRightDebut)+cellRightDebut.length(),row.indexOf(cellEnd,row.indexOf(cellRightDebut))).trim();
					String german = germanPart;
					String germanNote = "";
					String french = frenchPart;
					String frenchNote = "";
					if( german.contains(lilNoteDebut))
					{
						int iDeb = german.indexOf(lilNoteDebut);
						int iEnd = german.indexOf(lilNoteEnd)+lilNoteEnd.length();
						String note = german.substring(iDeb,iEnd);
						german = german.replace(note, "").trim();
						germanNote = note.substring(lilNoteDebut.length(),note.indexOf(lilNoteEnd)).trim();
					}
					if( french.contains(lilNoteDebut))
					{
						int iDeb = french.indexOf(lilNoteDebut);
						int iEnd = french.indexOf(lilNoteEnd)+lilNoteEnd.length();
						String note = french.substring(iDeb,iEnd);
						french = french.replace(note, "").trim();
						frenchNote = note.substring(lilNoteDebut.length(),note.indexOf(lilNoteEnd)).trim();
					}
					
					lastEntry = new TranslateEntry(french, frenchNote, german, germanNote);
					list.add(lastEntry);
					//out.print("German: " + german + " " + germanNote);
					//out.println("          French: " + french + " " + frenchNote);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public static List<LearningEntry> getLesson(int l){
		List<LearningEntry> list = new ArrayList<LearningEntry>();
		List<TranslateEntry> entries = getLessonContent(l);
		for(TranslateEntry te : entries)
		{
			String french = (te.getFrenchTranslation() + " " + te.getFrenchNote()).trim();
			String german = te.getGermanTranslation().trim();
			boolean keep = true;
			for(String r : removeFR[l-1])
				if(r.equals(french))
					keep = false;
			if(keep)
			{
				for(ModifyEntry me : changeFRFR[l-1])
					if(me.getKey().equals(french))
						french = me.getValue();
				for(ModifyEntry me : changeDEDE[l-1])
					if(me.getKey().equals(german))
						german = me.getValue();
				list.add(new LearningEntry(french.replace(" qc", " (le verbe)"), german.replace("etw. ", "")));
			}
		}
		return list;
	}
}
